/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hirt;

import java.awt.Point;
import java.nio.ByteBuffer;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * RequestCreateActions are used by the client to request an item be added to the scene
 * Can be used to request additional responders
 * Or add items as a editor
 * @author zamille
 */
public class RequestCreateAction extends Action
{
    /** The type of item to be created, i.e. cone,ambulence,etc*/
    String type;
    /** The Class of item to be created, i.e. Item2D,Automobile,etc*/
    String Class;
    /** The ID of the created items owner*/
    int ownerID;
    /** The position of the created item*/
    Point position;
    /** The amount of the item that need to be made*/
    int amount;


    /**
     *
     * @param type
     * @param Class
     * @param ownerID
     * @param x
     * @param y
     */
    public RequestCreateAction(String type, String Class, int ownerID, int x, int y)
    {
        this.type = type;
        this.Class = Class;
        this.ownerID = ownerID;
        position = new Point(x,y);
        amount = 1;
        super.messageType = Message.REQUEST_CREATE_MESSAGE;
    }

    /**
     *
     * @param type
     * @param Class
     * @param ownerID
     * @param x
     * @param y
     * @param amount
     */
    public RequestCreateAction(String type, String Class, int ownerID, int x, int y, int amount)
    {
        this(type, Class, ownerID,  x,  y);
        this.amount = amount;
    }

    /**
     *
     * @param type
     * @param Class
     * @param ownerID
     * @param amount
     */
    public RequestCreateAction(String type, String Class, int ownerID, int amount)
    {
        this(type, Class, ownerID,  0,  0, amount);
    }

    /**
     * Builds an Action from a byte stream, presumably created by an action across the network
     * this action should be identical to that one
     * @param bytes
     * @param items: Master Item List used to build the action
     */
    public RequestCreateAction(ByteBuffer bytes, Vector<Item> items)
    {
        fromBytes(bytes,items);
        super.messageType = Message.REQUEST_CREATE_MESSAGE;
    }

    /**
     * Builds an Action from a byte stream, presumably created by an action across the network
     * this action should be identical to that one
     * @param bytes
     * @param items: Master Item List used to build the action
     * @return what's left of the parem bytes after this methods completes
     */
    @Override
    protected ByteBuffer fromBytes(ByteBuffer bytes, Vector<Item> items)
    {
        try {
            ownerID = bytes.getInt(0);   //it uses the inherited method to build
            position = ByteChanger.getPoint(bytes, 4);  //the inherited members from the ByteBuffer
            amount = bytes.getInt(12);
            int length = bytes.getInt(16);
            type = ByteChanger.getString(bytes, 20, length);
            bytes = ByteChanger.truncate(bytes, 20 + length);
            Class = ByteChanger.getString(bytes, 0);

             bytes = ByteChanger.truncate(bytes, Class.length());

            return bytes;
        } catch (Exception ex) {
            Logger.getLogger(RequestCreateAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        return bytes;

    }

    /**
     * Returns a ByteBuffer representation of this Action
     * Used to send actions over the network
     * @return ByteBuffer
     */
    @Override
    public ByteBuffer toByte()
    {
        ByteBuffer buf = ByteBuffer.allocate(74);
        buf.putInt(ownerID);
        buf.put(ByteChanger.pointToByte(position));
        buf.putInt(amount);
        buf.putInt(type.length());
        buf.put(ByteChanger.stringToByte(type));
        buf.putInt(Class.length());
        buf.put(ByteChanger.stringToByte(Class));

        return buf;
    }

}
